package com.huawei.petSystemdemo.data;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huawei.petSystemdemo.bean.Pets;
import com.huawei.petSystemdemo.bean.Shop;
import com.huawei.petSystemdemo.utils.MyRequest;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

public class PetRepository {

    public static void getAll(int current, int size, Consumer<List<Pets>> callback) {
        Map<String, Object> body = new HashMap<>();
        body.put("size", size);
        body.put("current", current);
        MyRequest.get("/front/pet/getAll", body, json -> {
            JSONObject obj = JSON.parseObject(json);
            boolean success = obj.getBoolean("success");
            if (success)
            {
                List<Pets> petsList = obj.getJSONArray("data").toJavaList(Pets.class);
                if (callback != null)
                    callback.accept(petsList);
            }
            else
            {


            }
        });
    }

    public static void getByPetName(String petName,  Consumer<List<Pets>> callback) {
        Map<String, Object> res = new HashMap<>();
        res.put("name", petName);

        MyRequest.get("/pet/getByPetName", res, json -> {
            JSONObject obj = JSON.parseObject(json);
            boolean success = obj.getBoolean("success");
            if (success)
            {
                List<Pets> petsList = obj.getJSONArray("data").toJavaList(Pets.class);
                if (callback != null)
                    callback.accept(petsList);
            }
            else
            {
////
            }
        });

    }

    public static void getByShopId(int current, int size, int shopId, Consumer<List<Pets>> callback) {
        Map<String, Object> res = new HashMap<>();
        res.put("current", current);
        res.put("size", size);
        res.put("shopId", shopId);
        MyRequest.get("/pet/getByShopId", res, json -> {
            JSONObject obj = JSON.parseObject(json);
            boolean success = obj.getBoolean("success");
            if (success)
            {
                List<Pets> petsList = obj.getJSONArray("data").toJavaList(Pets.class);
                if (callback != null)
                    callback.accept(petsList);
            }
            else
            {
////
            }
        });
    }

}
